<template>
  <div class="password">
    <el-tabs class="DF-el_tabs">
      <el-tab-pane label="修改监考员信息">
        <el-col :span="18">
          <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px">
            <el-form-item label="监考员代码" prop="jkydm">
              <el-input v-model="dataForm.jkydm" placeholder="监考员代码" readOnly />
            </el-form-item>
            <el-form-item label="监考员密码" prop="jkymm">
              <el-input v-model="dataForm.jkymm" placeholder="监考员密码" show-password />
            </el-form-item>
            <el-form-item label="重复密码" prop="cfjkymm">
              <el-input v-model="dataForm.cfjkymm" placeholder="重复监考员密码" show-password />
            </el-form-item>
            <el-form-item label="姓名" prop="jkyxm">
              <el-input v-model="dataForm.jkyxm" placeholder="用监考员姓名"  />
            </el-form-item>
            <el-form-item label="联系电话" prop="jkylxdh">
              <el-input v-model="dataForm.jkylxdh" placeholder="联系电话"  />
            </el-form-item>
            <el-form-item>
              <el-button @click="goBack">{{$t('common.cancelButton')}}</el-button>
              <el-button type="primary" @click="dataFormSubmit()">保 存</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import md5 from 'js-md5';
import { updateLoginInfo, getLoginInfo } from '@/api/user/userSetting'
import df from '@/utils/df'
import tools from '@/utils/tools'
import { mapGetters } from 'vuex'
const basic = tools.basic
export default {
  computed: {
    ...mapGetters(['userInfo'])
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('新密码不能为空'));
      } else {
        if (this.dataForm.cfjkymm !== '') {
          this.$refs.dataForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.dataForm.jkymm) {
        callback(new Error('两次密码输入不一致'));
      } else {
        callback();
      }
    };
    return {
      dataForm: {
        jkydm: '',
        jkymm: '',
        cfjkymm: '',
        jkyxm: '',
        jkylxdh: ''
      },
      dataRule: {
        jkyxm: [
          { required: true, message: '请输入监考员姓名', trigger: 'blur' }
        ],
        jkymm: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        cfjkymm: [
          { required: true, message: '重复密码不能为空', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        jkylxdh: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getUserLoginInfo ()
    this.dataForm = basic.partData((this.userInfo), [
      'jkydm',
      'jkymm',
      {cfjkymm: 'jkymm'},
      'jkyxm',
      'jkylxdh'
    ])
    console.log(this.dataForm)
  },
  methods: {
    goBack () {
      this.$router.back()
    },
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          /*{
            oldPassword: this.dataForm.oldPassword,
            password: this.dataForm.password//md5(this.dataForm.password),
          }*/
          updateLoginInfo(this.dataForm).then(res => {
            this.$message({
              message: res.Result.Msg,
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.$store.dispatch('user/resetToken').then(() => {
                  this.$router.push(`/login`)
                })
              }
            })
          })
        }
      })
    },
    getUserLoginInfo () {
      this.dataForm = this.$store.getters.userInfo
      this.dataForm.yhmm = ''
      // console.log(this.$store.getters.userInfo)
      /*getLoginInfo(df.storageGet('loginInfo')).then(res => {
        this.dataForm = basic.partData(res.Response, [
          'yhdm',
          'mm',
          'cfmm',
          'yhmc',
          'lxdh'])
      })*/
    },
    changeImg() {
      let timestamp = Math.random()
      this.timestamp = timestamp
      this.imgUrl = `/api/file/ImageCode/${timestamp}`
    }
  }

}
</script>
<style lang="scss" scoped>
.password {
  height: 100%;
  overflow: hidden;
  >>> .el-tabs__nav-scroll {
    padding-top: 0 !important;
  }
}
</style>
